<template>
  <div class="page-header-index-wide">
    <a-row :gutter="24">
      <a-col :sm="24" :md="12" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="问题总数" :total="main.questionCount">
          <div>
            <trend style="margin-right: 16px;">
              <span slot="term">昨日</span>
              {{main.questionYesterdayCount}}个
            </trend>
            <trend >
              <span slot="term">今日</span>
              {{main.questionTodayCount}}个
            </trend>
          </div>
          <template slot="footer"></template>
        </chart-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="学员总数" :total="main.userCount | NumberFormat">
          <div>
            <mini-bar :height="40" :dataSource="ageZhuData"/>
          </div>
          <template slot="footer"></template>
        </chart-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="方案总数" :total="main.planCount">
          <div>
            <mini-area :height="40" :dataSource="everyDayPlanZhu"/>
          </div>
          <template slot="footer">
          </template>
        </chart-card>
      </a-col>
    </a-row>

    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard " style="padding-top: 21px;">
        <a-row>
          <a-col :xl="16" :lg="12" :md="12" :sm="24" :xs="24">
            <bar title="14天问题" :dataSource="barData"/>
          </a-col>
          <a-col :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
            <rank-list title="高频问题排行" :list="rankList"/>
          </a-col>
        </a-row>
      </div>
    </a-card>

    <a-row>
      <a-col :span="24">
        <a-card :loading="loading" :bordered="false" title="最近一周访问量统计" :style="{ marginTop: '24px' }">
          <a-row>
            <a-col :span="6">
              <head-info title="今日IP" :content="loginfo.todayIp"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="environment" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="今日访问" :content="loginfo.todayVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="team" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="总访问量" :content="loginfo.totalVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="rise" style="font-size: 24px"  />
              </a-spin>
            </a-col>
          </a-row>
          <line-chart-multid :fields="visitFields" :dataSource="visitInfo"></line-chart-multid>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniArea from '@/components/chart/MiniArea'
  import MiniBar from '@/components/chart/MiniBar'
  import MiniProgress from '@/components/chart/MiniProgress'
  import RankList from '@/components/chart/RankList'
  import Bar from '@/components/chart/Bar'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'

  import Trend from '@/components/Trend'
  import { getLoginfo,getVisitInfo, getIndex } from '@/api/api'

  const rankList = []
  const barData = []
  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniArea,
      MiniBar,
      MiniProgress,
      RankList,
      Bar,
      Trend,
      LineChartMultid,
      HeadInfo
    },
    data() {
      return {
        loading: true,
        center: null,
        rankList,
        barData,
        main: {},
        ageZhuData: [],
        everyDayPlanZhu: [],
        loginfo:{},
        visitFields:['ip','visit'],
        visitInfo:[],
        indicator: <a-icon type="loading" style="font-size: 24px" spin />
      }
    },
    created() {
      setTimeout(() => {
        this.loading = !this.loading
      }, 1000)
      this.initLogInfo();
    },
    methods: {
      initLogInfo () {
        var self = this
        getLoginfo(null).then((res)=>{
          if(res.success){
            Object.keys(res.result).forEach(key=>{
              res.result[key] =res.result[key]+""
            })
            this.loginfo = res.result;
          }
        })
        getIndex(null).then(res=>{
          if(res.success){
            this.main = res.result

            // 年龄分布
            self.ageZhuData = []
            if(res.result.traineeAgeDistDTOList != null) {
              res.result.traineeAgeDistDTOList.forEach(function(v) {
                self.ageZhuData.push({
                  ['x']: v.bracketStart+'~'+v.bracketEnd,
                  ['y']: v.persons
                })
              })
            }
            // 七天方案分布
            self.everyDayPlanZhu = []
            var everyDayPlanZhuDate = []
            if(res.result.everyDayPlanMap != null) {
              var everyDayPlanList = JSON.stringify(res.result.everyDayPlanMap).replace("{", '').replace('}', '').split(',');
              for(let key of everyDayPlanList){
                everyDayPlanZhuDate.push({
                  ['x']: key.split(':')[0].replace('"', '').replace('"', ''),
                  ['y']: key.split(':')[1]
                })
              }
              self.everyDayPlanZhu = self.sortList(everyDayPlanZhuDate)
            }
            //14天问题
            var everyDayQuestionZhuDate = []
            if(res.result.everyDayQuestionMap != null){
              var everyDayQuestionList = JSON.stringify(res.result.everyDayQuestionMap).replace("{", '').replace('}', '').split(',');
              for(let key of everyDayQuestionList){
                everyDayQuestionZhuDate.push({
                  ['x']: key.split(':')[0].replace('"', '').replace('"', ''),
                  ['y']: parseInt(key.split(':')[1])
                })
              }
              self.barData = self.sortList(everyDayQuestionZhuDate)
            }
            //高频问题排序
            if(res.result.questionTypes != null){
              for (let item of res.result.questionTypes) {
                rankList.push({
                  name: item.name,
                  total: item.typeCount
                })
              }
            }
          }
        })
        getVisitInfo().then(res=>{
          if(res.success){
             this.visitInfo = res.result;
           }
         })
      },
      sortList(lists){                // lists传的是数组
        return lists.sort((a, b) => {
          return a['x'].localeCompare(b['x'])     // grapheme为字母对应的属性名
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    }
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    salesCard-padding{
      padding-top: 10px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>